/**
 * Created by CHENCO7 on 9/1/2017.
 */
const orderFacade = require('../facade/order.server.facade');
const _ = require('lodash');

module.exports = {
    changeOrderStatusByOrderId: changeOrderStatusByOrderId,
    findAllOrdersByMerchantId: findAllOrdersByMerchantId,
    findOrdersByMerchantIdByStatus:findOrdersByMerchantIdByStatus
};

function findAllOrdersByMerchantId(req, res, next) {
    let merchantId = req.merchantId;
    orderFacade.findOrdersByMerchantId(merchantId,(error, result) => {
        if(error){
            return next(new Error(error));
        }else {
            return res.json(result);
        }
    })
}

function findOrdersByMerchantIdByStatus(req,res,next){
    let merchantId = req.merchantId;
    let status = req.params.status;
    if(_.isEqual(status,'all')){
        orderFacade.findOrdersByMerchantId(merchantId,(error, result) => {
            if(error){
                return next(new Error(error));
            }else {
                return res.json(result);
            }
        });
    }else{
        orderFacade.findOrdersByMerchantIdByStatus(merchantId,status,(error, result) => {
            if(error){
                return next(new Error(error));
            }else {
                return res.json(result);
            }
        });
    }

}

function changeOrderStatusByOrderId(req, res, next) {
    let merchantId = req.merchantId;
    let order = req.body;
    console.log(order);
    orderFacade.updateOrderStatusById(order,merchantId, (error, result) => {
        if(error){
            return next(new Error(error));
        }else {
            return res.json(result);
        }
    })
}
